<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/UserCom.php");
require_once(dirname(__FILE__) . "/../../common/Common.php");

$ragioncom = new RagionCom();
$usercom = new UserCom();
$common = new Common();

$communityarr = $ragioncom->getRagionByType(2);
$community = array();
if (!empty($communityarr)) {
    foreach ($communityarr as $k => $v) {
        $community[$k]['id'] = $v['id'];
        $community[$k]['name'] = $v['name'];
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <style type="text/css">
            body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
        </style>
        <link href="../public/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
        <link href="../public/css/font-awesome.css?v=4.4.0" rel="stylesheet">
        <link href="../public/css/animate.css" rel="stylesheet">
        <link href="../public/css/style.css?v=4.1.0" rel="stylesheet">
        <script src="../public/js/jquery.min.js?v=2.1.4"></script>
        <script src="../public/js/bootstrap.min.js?v=3.3.6"></script>
        <script src="../public/js/plugins/layer/layer.min.js"></script>
        <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=tXfVciWna9Y5Wni6D37u5qkvYAc4rNWG"></script>
        <title>新增爬楼机</title>
    </head>
    <body>
        <div id="allmap" style="width:60%;float:left;"></div>
        <div class="mapleft" style="width:40%;float:left;">
            <div class="ibox-content">
                <div class="form-horizontal">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">备注</label>
                        <div class="col-sm-10">
                            <input type="text" placeholder="描述机器，用于区分机器" id="des" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label>社区选择</label>
                        <select class="form-control" id="communityid" style="height: 35px;">
                            <?php foreach ($community as $v) { ?>
                                <option value="<?= $v['id'] ?>" ><?= $v['name'] ?></option>
                            <?php } ?> 
                        </select>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">经度</label>
                        <div class="col-sm-10">
                            <input type="text" disabled="" placeholder="地图经纬度" id="longitude" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">维度</label>
                        <div class="col-sm-10">
                            <input type="text" disabled="" placeholder="地图经纬度" id="latitude" class="form-control">
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <div class="col-sm-4 col-sm-offset-2">
                            <button class="btn btn-primary" type="button" id="submitform">新增机器</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>
<script type="text/javascript">
    var point;
    var marker;
    // 百度地图API功能
    var map = new BMap.Map("allmap");    // 创建Map实例
    map.centerAndZoom(new BMap.Point(120.58531, 31.298886), 11);  // 初始化地图,设置中心点坐标和地图级别
    //添加地图类型控件
    map.addControl(new BMap.MapTypeControl({
        mapTypes: [
            BMAP_NORMAL_MAP,
            BMAP_HYBRID_MAP
        ]}));
    map.setCurrentCity("苏州");          // 设置地图显示的城市 此项是必须设置的
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
    var geocoder = new BMap.Geocoder();
    map.addEventListener("click", function (e) {
        geocoder.getLocation(e.point, function (rs) {
            console.log(rs);
            $("#longitude").val(rs.point.lng);
            $("#latitude").val(rs.point.lat);
            map.removeOverlay(marker);
            point = new BMap.Point(rs.point.lng, rs.point.lat);
            marker = new BMap.Marker(point);        // 创建标注    
            map.addOverlay(marker);                     // 将标注添加到地图中 
        });
    });
    $("#submitform").click(function () {
        var longitude = $("#longitude").val();
        var latitude = $("#latitude").val();
        var des = $("#des").val();
        var communityid = $("#communityid").val();
        if (longitude == "" || latitude == "") {
            layer.msg('请标记地图,选择机器位置', {
                time: 2000, //20s后自动关闭
                btn: ['OK']
            });
            return;
        }
        if (des == "" || communityid == "") {
            layer.msg('请填写项目', {
                time: 2000, //20s后自动关闭
                btn: ['OK']
            });
            return;
        }
        var datastr = "cont=1&longitude=" + longitude + "&latitude=" + latitude + "&des=" + des + "&communityid=" + communityid;
        $.ajax({
            url: "/../../controller/mechine.php",
            type: "POST",
            data: datastr,
            success: function (e) {
                var result = eval("(" + e + ")");
                if (result.code == 0) {
                    layer.msg('添加成功', {
                        time: 2000, //20s后自动关闭
                        btn: ['OK']
                    });
                } else {
                    layer.msg('添加失败请刷新重试', {
                        time: 2000, //20s后自动关闭
                        btn: ['OK']
                    });
                }
                return;
            }
        });
    });
</script>